package cn.rongdao.jrkabao.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.mifengkong.frtools.util.FRLog;
import com.mifengkong.frtools.util.FRString;

import org.json.JSONException;
import org.json.JSONObject;

import cn.mifengkong.appcommon.util.ClassUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.ui.main.MainActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

public class GeTuiPushReceiver extends BroadcastReceiver {

    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */
    public static StringBuilder payloadData = new StringBuilder();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == intent) {
            return;
        }
        Bundle bundle = intent.getExtras();
        if (null == bundle) {
            return;
        }
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传数据
                byte[] payload = bundle.getByteArray("payload");

                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");

                // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
                FRLog.debug("第三方回执接口调用" + (result ? "成功" : "失败"));
                if (payload != null) {
                    String data = new String(payload);
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        String url = jsonObject.getString("url");
                        if (!FRString.isEmpty(url)) {
                            if (url.startsWith("http://") || url.startsWith("https://")) {
                                WebViewActivity.start(context, url);
                            } else if (url.startsWith("kamao://")) {
                                String activityName = url.substring(url.lastIndexOf("/") + 1);
                                if (activityName.equals("FragmentRemind")) {
                                    MainActivity.Companion.start(context, true);
                                    return;
                                }
                                Class aClass = ClassUtil.getClassByName(context, BuildConfig.APPLICATION_ID, activityName);
                                if (null != aClass) {
                                    context.startActivity(new Intent(context, aClass).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    payloadData.append(data);
                    payloadData.append("\n");
                }
                break;

            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                // 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
                String cid = bundle.getString("clientid");
                FRLog.debug("cid----->" + cid);
                break;
            case PushConsts.GET_SDKONLINESTATE:
                boolean online = bundle.getBoolean("onlineState");
                FRLog.debug("GetuiSdkDemo----->online=" + online);
                break;

            case PushConsts.SET_TAG_RESULT:
                String sn = bundle.getString("sn");
                String code = bundle.getString("code");

                String text = "设置标签失败, 未知异常";
                switch (Integer.valueOf(code)) {
                    case PushConsts.SETTAG_SUCCESS:
                        text = "设置标签成功";
                        break;

                    case PushConsts.SETTAG_ERROR_COUNT:
                        text = "设置标签失败, tag数量过大, 最大不能超过200个";
                        break;

                    case PushConsts.SETTAG_ERROR_FREQUENCY:
                        text = "设置标签失败, 频率过快, 两次间隔应大于1s";
                        break;

                    case PushConsts.SETTAG_ERROR_REPEAT:
                        text = "设置标签失败, 标签重复";
                        break;

                    case PushConsts.SETTAG_ERROR_UNBIND:
                        text = "设置标签失败, 服务未初始化成功";
                        break;

                    case PushConsts.SETTAG_ERROR_EXCEPTION:
                        text = "设置标签失败, 未知异常";
                        break;

                    case PushConsts.SETTAG_ERROR_NULL:
                        text = "设置标签失败, tag 为空";
                        break;

                    case PushConsts.SETTAG_NOTONLINE:
                        text = "还未登陆成功";
                        break;

                    case PushConsts.SETTAG_IN_BLACKLIST:
                        text = "该应用已经在黑名单中,请联系售后支持!";
                        break;

                    case PushConsts.SETTAG_NUM_EXCEED:
                        text = "已存 tag 超过限制";
                        break;

                    default:
                        break;
                }
                FRLog.debug("GetuiSdkDemo" + "settag result sn = " + sn + ", code = " + code + ", text=" + text);
                break;
            case PushConsts.THIRDPART_FEEDBACK:

                break;

            default:
                break;
        }
    }
}
